<template>
<!-- 搜索结果 -->
  <div>
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="onLoad"
      >
  <van-cell v-for="(item,index) in list" :key="index" :title="item.title" />
</van-list>
  </div>
</template>

<script>
import { getSearch } from '@/api/search'
export default {
  name: 'ResultIndex',
  props: {
    searchText: {
      type: String,
      required: true
    }
  },
  data () {
    return {
      list: [],
      loading: false,
      finished: false,
      page: 1, // 页码
      perPage: 20 // 每页数量
    }
  },
  methods: {
    // 滚动加载
    async onLoad () {
      try {
        const { data } = await getSearch({
          page: this.page, // 页码
          per_page: this.perPage, // 每页大小
          q: this.searchText// 搜索关键字
        })
        console.log(data)
        this.list.push(...data.data.results)
        this.loading = false
        // 判断是否还有数据
        if (data.data.results.leng) {
          // 页数加一
          this.page++
        } else {
          // 结束条件
          this.finished = true
        }
      } catch (err) {
        console.log('请求失败', err)
      }
    }

  }
}

</script>

<style>
</style>
